package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.soms.dto.SubjectsDto;
import com.dingreading.cloud.soms.entity.Subjects;
import com.dingreading.cloud.soms.entity.table.SubjectsTableDef;
import com.mybatisflex.core.query.QueryWrapper;
import com.dingreading.cloud.soms.entity.Subjects;
import com.dingreading.cloud.soms.mapper.SubjectsMapper;
import com.dingreading.cloud.soms.service.SubjectsService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 *  服务层实现。
 *
 * @author EDY
 * @since 2025-10-29
 */
@Service
public class SubjectsServiceImpl extends BaseServiceImpl<SubjectsMapper, Subjects, SubjectsTableDef> implements SubjectsService {

    @Override
    protected SubjectsTableDef getTable() {
        return SubjectsTableDef.subjects;
    }

    @Override
    public Subjects getByUid(String projectUid) {
        return getMapper().selectOneByCondition(table.uid.eq(projectUid));
    }

    @Override
    public List<SubjectsDto> getBySubjectMainId(Long subjectMainId) {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.subjectMainId.eq(subjectMainId));
        return getMapper().selectListByQueryAs(wrapper, SubjectsDto.class);
    }

    @Override
    public List<Subjects> getBySubjectIds(Set<Long> uniqueSubjectIds) {
        QueryCondition queryCondition = QueryCondition.createEmpty()
                .and(table.id.in(uniqueSubjectIds))
                .and(table.enabled.eq(1));
        return getMapper().selectListByCondition(queryCondition);
    }

    @Override
    public Subjects findByMainSubjectName(String findMainSubjectName) {
        return getMapper().selectOneByCondition(table.subjectName.eq(findMainSubjectName));
    }

    @Override
    public Subjects findByMainSubjectIdAndSubjectName(Long id, String findSubjectName) {
        return getMapper().selectOneByCondition(table.subjectName.eq(findSubjectName).and(table.parentId.eq(id)));
    }
}
